from django.contrib import admin

from .models import Message


@admin.register(Message)
class MessageAdmin(admin.ModelAdmin):
    list_display = ('sender', 'receiver', 'content_display', 'time_display', 'read')
    empty_value_display = '公共'

    def has_add_permission(self, request):
        return False

    def has_change_permission(self, request, obj=None):
        return False

    @admin.display(description='内容')
    def content_display(self, obj):
        content = obj.content
        return content[:50] + '……' if len(content) > 50 else content

    @admin.display(description='时间')
    def time_display(self, obj):
        time = obj.time
        return time.strftime('%Y年%m月%d日 %H时%M分%S秒')
